package com.cskaoyan.config.shiro;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * ClassName: CustomSessionManager
 * Description: 登录成功 → 获得SessionId → 把SessionId返回给前端
 * 前端发送请求的时候把SessionId一起携带过来（请求头） 通过SeesionManager获得请求头携带的SessionId拿到对应的Session
 *
 * ★我们需要重写的方法 → 获取SessionId的方法
 *
 * date: 2022/6/6 17:30
 *
 * @author bruces
 * @since JDK 8
 */
public class CustomSessionManager extends DefaultWebSessionManager {
    private static final String HEADER = "X-CskaoyanMarket-Admin-Token";
    private static final String WX_HEADER = "X-CskaoyanMarket-Token";
    @Override
    protected Serializable getSessionId(ServletRequest srequest, ServletResponse response) {
        HttpServletRequest request = (HttpServletRequest) srequest;
        String sessionId = request.getHeader(HEADER);
        if (sessionId != null && !"".equals(sessionId)) {
            return sessionId;
        }
        String sessionId2 = request.getHeader(WX_HEADER);
        if (sessionId2 != null && !"".equals(sessionId2)) {
            return sessionId2;
        }
        return super.getSessionId(srequest, response);
    }
}
